﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;

using System.ComponentModel;

namespace MSAP.ObjectsStore
{
    public class RecordSetBase<TRECORD> : IRecordSet<TRECORD>,
                                          INotifyPropertyChanged
                                          where TRECORD : RecordBase<TRECORD>, new()
    {
        public TRECORD CreateRecord()
        {
            TRECORD rec = RecordSetProvider.CreateRecord();
            if (rec != null)
                Records.Add(rec);
            return rec;
        }

        public TRECORD LoadRecord(Guid _Oid)
        {
            TRECORD rec = RecordSetProvider.LoadRecord(_Oid);
            if (rec != null)
                Records.Add( rec );
            return rec;
        }

        public void AddRecord(TRECORD _theRecord)
        {
            Records.Add(_theRecord);
            RaisePropertyChanged("Records");
        }

        public void DeleteRecord(Guid _Oid)
        {
            TRECORD rec = GetRecord(_Oid);
            if (rec == null)
                return;
            Records.Remove(rec);
            RecordSetProvider.DeleteRecord(_Oid);
            RaisePropertyChanged("Records");
        }

        public TRECORD GetRecord(Guid _Oid)
        {
            return RecordSetProvider.GetRecord(_Oid);
        }

        public void Save(string sFileName)
        {
            List<Guid> lstGuid = new List<Guid>();
            foreach (TRECORD rec in Records)
            {
                if (rec.IsDirty() == true)
                    lstGuid.Add(rec.Oid);
            }
            Save(sFileName, lstGuid);
        }

        public void Save(string sFileName, List<Guid> lstGuid)
        {
            RecordSetProvider.Save(sFileName, lstGuid);
        }

        public void Load(string sFileName)
        {
            RecordSetProvider.Load(sFileName);
            Records.Clear();
            foreach (TRECORD rec in RecordSetProvider.Records)
                Records.Add(rec);
            RaisePropertyChanged("Records");
        }

        public ObservableCollection<TRECORD> Records
        {
            get
            {
                if (m_Records == null)
                    m_Records = new ObservableCollection<TRECORD>();
                return m_Records;
            }
            private set
            {
                m_Records = value;
            }
        }

        #region INotifyPropertyChanged Members
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region IOC
        private static IRecordSet<TRECORD> m_iocRecordSetProvider;
        public static IRecordSet<TRECORD> RecordSetProvider 
        {
            get
            {
                return m_iocRecordSetProvider;
            }
            set
            {
                m_iocRecordSetProvider = value;
            }
        }
        #endregion

        #region Fields
        private ObservableCollection<TRECORD> m_Records;
        #endregion
    }
}
